/**
 * Created by LeungZ on 2016/10/17.
 */
class Deployment {
    constructor($scope, ctgNotification, nodeSrv, appSrv, $state) {
        "ngInject";
        $scope.$parent.vm.step = 2;
        this._notification = ctgNotification;
        this.serviceList = [];
        this._fData = $scope.$parent.vm.fData;
        this._appSrv = appSrv;
        this._state = $state;

        this.appImgList = $scope.$parent.vm.appImgList;
        this.groupList = [];

        if (this._fData.cluster) {
            this.clusterCode=this._fData.cluster.clusterCode;
            nodeSrv.getGroupList({perPage: 99, page: 1, code: this._fData.cluster.clusterCode})
                .then(data=> {
                    this.groupList = data.list;
                    angular.forEach(this.serviceList, service=> {
                        service.groupList = this.groupList;
                    });
                }).catch(data=> {
                    this._notification.error('获取宿主机组详情数据有误');
                })
        }

        if (this._fData.hasOwnProperty('services')) {
            this.serviceList = this._fData.services;
        } else this.addService();
    }

    addService() {
        this.serviceList.push({
            opened: false,
            appImgList: this.appImgList,
            groupList: this.groupList,
            checkServiceValidate: this.checkServiceValidate.bind(this)
        });
    }

    checkServiceValidate(baseCtrl) {
        for (var i in this.serviceList) {
            if (this.serviceList[i].baseCtrl.podAppName == baseCtrl.podAppName && baseCtrl !== this.serviceList[i].baseCtrl) {
                return baseCtrl.baseForm.podAppName.$setValidity('duplicate', false);
            }
        }
        baseCtrl.baseForm.podAppName.$setValidity('duplicate', true);
    }

    deleteService(index) {
        this.serviceList.splice(index, 1);
    }

    expendForm(elem) {
        if(this.loading) return;

        if (elem.opened) {
            elem.advCtrl.domainForm.$submitted = true;
            elem.advCtrl.autoScaleForm.$submitted = true;
            elem.baseCtrl.baseForm.$submitted = true;
            if (elem.baseCtrl.baseForm.$valid && elem.advCtrl.domainForm.$valid
                && elem.advCtrl.autoScaleForm.$valid) elem.opened = false;
        } else {
            elem.opened = true;
        }

    }

    next() {
        for (var i in this.serviceList) {
            if (this.serviceList[i].baseCtrl.baseForm.$invalid ||
                this.serviceList[i].advCtrl.autoScaleForm.$invalid ||
                this.serviceList[i].advCtrl.domainForm.$invalid) return;

            this.serviceList[i].opened = false;
            this.serviceList[i].namespace=this._fData.lessee;//加上租户编码
            this.serviceList[i].clusterCode=this.clusterCode;//
        }

        this.loading = true;
        this._fData.services = this.serviceList;
        this._fData.app.namespace=this._fData.lessee;
        this._appSrv.getYaml(this.serviceList).then(data=> {
            this._fData.yJsonList = data;
            console.log(JSON.stringify(data));
            this._state.go('app-service-publish.release');
        }).catch((data)=>{
            if(data.resultCode==1200){
                this._notification.error(data.resultMsg);
            }else{
            this._notification.error("加载Yaml配置失败.");
            }
        }).finally(()=>{
            this.loading = false;
        })

    }
}
export default Deployment